//
//  AppDelegate.m
//  NewMyMusicAPP
//
//  Created by 11_wyq on 15/6/19.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible ];
    
    
    
    //后台运行
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    //让app支持接受远程控制事件
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

       
    
   


//分享
   [UMSocialData setAppKey:@"558b68a867e58ee2a4001769"];

    
    
    
    
    MainTabBarController *TabBatC = [[MainTabBarController alloc]init];
    drawerViewController *leftMenuViewController = [[drawerViewController alloc] init];
   
    
    RESideMenu *sideMenuViewController = [[RESideMenu alloc] initWithContentViewController:TabBatC
                                                                    leftMenuViewController:leftMenuViewController
                                                                   rightMenuViewController:nil];
    sideMenuViewController.backgroundImage = [UIImage imageNamed:@"backGround_4.jpg"];
    sideMenuViewController.menuPreferredStatusBarStyle = 1; // UIStatusBarStyleLightContent
    sideMenuViewController.delegate = self;
    sideMenuViewController.contentViewShadowColor = [UIColor blackColor];
    sideMenuViewController.contentViewShadowOffset = CGSizeMake(0, 0);
    sideMenuViewController.contentViewShadowOpacity = 0.6;
    sideMenuViewController.contentViewShadowRadius = 12;
    sideMenuViewController.contentViewShadowEnabled = YES;
    self.window.rootViewController = sideMenuViewController;
    
    
    // 设置状态栏颜色 (黑底白字)
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
#pragma mark -- 监听当前的网络情况    
    //监听网络状态
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];
    self.conn = [Reachability reachabilityForInternetConnection];
    //开始监听
    [self.conn startNotifier];
    //程序运行时监听网络状态
    [self checkNetworkState];
    
    
    
    
    //开机动画
   // static BOOL number=0;
    NSUserDefaults *library=[NSUserDefaults standardUserDefaults];
   
    if ([library objectForKey:@"libraryNumber"]==nil){
        
     //   [library setBool:number forKey:@"libraryNumber"];
        
        NSArray *coverImageNames = @[@"IMG_1154_2.png", @"IMG_1155_2.png", @"IMG_1166_2.png"];
        NSArray *backgroundImageNames = @[@"IMG_1154.png", @"IMG_1155.png", @"IMG_1166.png"];
       // NSLog(@"走没走");
        
        self.introductionView = [[ZWIntroductionViewController alloc]initWithCoverImageNames:coverImageNames backgroundImageNames:backgroundImageNames];
        [self.window addSubview:self.introductionView.view];
        
        __weak AppDelegate *weakSelf = self;
        self.introductionView.didSelectedEnter = ^() {
            [weakSelf.introductionView.view removeFromSuperview];
            weakSelf.introductionView = nil;
            
            
        };
        
    }
    
    



    
 
    

    
    
    
    [_window release];

    // Override point for customization after application launch.
    return YES;
}



- (void)checkNetworkState
{
    // 1.检测wifi状态
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    // 2.检测手机是否能上网络(WIFI\3G\2.5G)
    Reachability *conn = [Reachability reachabilityForInternetConnection];
    // 3.判断网络状态
    if ([wifi currentReachabilityStatus] != NotReachable) { // 有wifi
        NSLog(@"有wifi");
        //NSLog(@"有网啦.......");
        [DataHandle shareHandleController].monitorNetwork = YES;
        [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"status"];
    } else if ([conn currentReachabilityStatus] != NotReachable) { // 没有使用wifi, 使用手机自带网络进行上网
        NSLog(@"使用手机自带网络进行上网");
        UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前使用的是手机网络,请注意流量" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [aler show];
        //NSLog(@"有网啦.......");
        [DataHandle shareHandleController].monitorNetwork = YES;
        [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"status"];
    } else { // 没有网络
        [[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"status"];
        NSLog(@"没有网络");
        [DataHandle shareHandleController].monitorNetwork = NO;

        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(conectionAction) userInfo:nil repeats:NO];
    }
}

- (void)networkStateChange
{
    [self checkNetworkState];
}

-(void)conectionAction
{
    CGRect frame = self.redView.frame;
    frame.origin.x = 414;
    self.redView.frame = frame;
}

#pragma mark --- 自动调用
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    if (event.type == UIEventTypeRemoteControl) {
        switch (event.subtype) {
            case UIEventSubtypeRemoteControlPlay:
                [[FMPlayerVC shareHandleController].audioPlayer resume]; // 播放
                break;
                
            case UIEventSubtypeRemoteControlPause:
                [[FMPlayerVC shareHandleController].audioPlayer pause];//暂停
                break;
                
            case UIEventSubtypeRemoteControlPreviousTrack:
                [[FMPlayerVC shareHandleController] lastSong]; // 播放上一曲按钮
                break;
                
            case UIEventSubtypeRemoteControlNextTrack:
                [[FMPlayerVC shareHandleController] NextMusic]; // 播放下一曲按钮
                break;
                
            default:
                break;
        }
    }
}




#pragma mark -
#pragma mark RESideMenu Delegate

- (void)sideMenu:(RESideMenu *)sideMenu willShowMenuViewController:(UIViewController *)menuViewController
{
    NSLog(@"willShowMenuViewController: %@", NSStringFromClass([menuViewController class]));
}

- (void)sideMenu:(RESideMenu *)sideMenu didShowMenuViewController:(UIViewController *)menuViewController
{
    NSLog(@"didShowMenuViewController: %@", NSStringFromClass([menuViewController class]));
}

- (void)sideMenu:(RESideMenu *)sideMenu willHideMenuViewController:(UIViewController *)menuViewController
{
    NSLog(@"willHideMenuViewController: %@", NSStringFromClass([menuViewController class]));
}

- (void)sideMenu:(RESideMenu *)sideMenu didHideMenuViewController:(UIViewController *)menuViewController
{
    NSLog(@"didHideMenuViewController: %@", NSStringFromClass([menuViewController class]));
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
 //即将进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
   
    [application beginBackgroundTaskWithExpirationHandler:nil];
    
    
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
